package com.ymatou.iapi.fundcommission.testcase;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.fundcommission.service.CommissionAmountGetCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.database.sqlwapper.YmtFundDimensionWapper;
import com.ymttest.utils.JsonBeanHelper;
/**
*获取佣金总额接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_CommissionAmountGet{
	private static CommissionAmountGetCall commissionamountgetCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取佣金总额接口");
	}

	@Before
	public void caseUp() {
		commissionamountgetCall=new CommissionAmountGetCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(commissionamountgetCall.getOpurl());
	}

	/**
	 * 接口返回信息公用错误信息部分断言（正向Case场景）
	 * @throws Exception
	 */	
	public static void commissionamountgetreturncode() throws Exception {
	    try {	    	
			//获取返回报文的数据
			String dataStr = commissionamountgetCall.getReturnData();
			//返回报文断言
			Logger.verifyNotNull(dataStr, "返回报文非空");			
			String Success =  new JSONObject(dataStr).getString("Success");
			Logger.verifyIsContains(true, Success, "\"Success\":true");
			String errMsg =  new JSONObject(dataStr).getString("ErrMsg");
			Logger.verifyIsNull(errMsg, "Msg等于空");
			String errCode =  new JSONObject(dataStr).getString("ErrCode");
			Logger.verifyIsNull(errCode, "ErrCode等于空");
			String body =  new JSONObject(dataStr).getString("Body");
			Logger.verifyIsNull(body, "Body等于空");
			String requestUrl =  new JSONObject(dataStr).getString("RequestUrl");
			Logger.verifyIsNull(requestUrl, "RequestUrl等于空");
	    	    	    	
	    }catch (Exception e) {  
	        e.printStackTrace();
	    }  

	}
	
	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CommissionAmountGet_001() {
		Logger.start(true,"获取佣金总额");
		try {
			//TODO 补全case代码
			YmtFundDimensionWapper ymtFundDimensionWapper = new YmtFundDimensionWapper();
	    	Map mnull1 = new HashMap();
			List<Map> getseller = ymtFundDimensionWapper.commissiongetsellerid(mnull1);
			int sellerid= (Integer) getseller.get(0).get("SellerId");
			String sellerloginid = (String) getseller.get(0).get("SellerLoginId");
			
			Map<String,Object> aa = new HashMap<String, Object>();
			aa.put("UserLoginId", sellerloginid);	
			aa.put("UserId", sellerid);	
			aa.put("BeginTime", "2015-01-01");	
			aa.put("EndTime", "2020-12-31");
			//调用接口json文件
			JSONObject commissionamountgetBean =  JsonBeanHelper.getJsonBean("/com.ymatou.iapi.fundcommission/CommissionAmountGet.json");
			commissionamountgetBean = JsonBeanHelper.updateJsonBean(commissionamountgetBean, aa);			
			commissionamountgetCall.setData(commissionamountgetBean);
			commissionamountgetCall.callService();
			String dataStr = commissionamountgetCall.getReturnData();			
			//SQL查询
	    	Map m2 = new HashMap();
	        m2.put("sellerid", sellerid);
	        m2.put("beginTime", "2015-01-01");
	        m2.put("endTime", "2020-12-31");
	        List<Map> commissionamountbysql = ymtFundDimensionWapper.commissionbysellerid(m2);
	        //断言
	        commissionamountgetreturncode();
	        //收佣总费用断言
	        BigDecimal subCommission = ((BigDecimal) commissionamountbysql.get(0).get("SubCommission")).setScale(2,BigDecimal.ROUND_HALF_UP);	//sql查询结果
	        Double tsc = (Double) new JSONObject(dataStr).getDouble("TotalSubCommission");	//取报文结果
	        BigDecimal totalSubCommission = new BigDecimal(tsc).setScale(2,BigDecimal.ROUND_HALF_UP);
	        Logger.verifyEquals(subCommission, totalSubCommission, "总收佣费用对比");
	        //退佣总费用断言
	        BigDecimal returnCommission = ((BigDecimal) commissionamountbysql.get(0).get("ReturnCommission")).setScale(2,BigDecimal.ROUND_HALF_UP);
	        Double rtc = (Double) new JSONObject(dataStr).getDouble("TotalReturnCommission");
	        BigDecimal totalReturnCommission = new BigDecimal(rtc).setScale(2,BigDecimal.ROUND_HALF_UP);
	        Logger.verifyEquals(returnCommission, totalReturnCommission, "总退佣费用对比");
	        //返佣总费用断言
	        BigDecimal refundCommission = ((BigDecimal) commissionamountbysql.get(0).get("RefundCommission")).setScale(2,BigDecimal.ROUND_HALF_UP);
	        Double rec = (Double) new JSONObject(dataStr).getDouble("TotalRefundCommission");
	        BigDecimal totalRefundCommission = new BigDecimal(rec).setScale(2,BigDecimal.ROUND_HALF_UP);
	        Logger.verifyEquals(refundCommission, totalRefundCommission, "总返佣费用对比");

			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}	
}